package com.csu.tingtian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.tingtian.common.CommonResponse;
import com.csu.tingtian.entity.LegalFarm;
import com.csu.tingtian.entity.ProvideProduce;
import com.csu.tingtian.persistence.LegalFarmMapper;
import com.csu.tingtian.persistence.ProduceCommentMapper;
import com.csu.tingtian.persistence.ProvideProduceMapper;
import com.csu.tingtian.service.ProduceCommentService;
import com.csu.tingtian.service.ProduceService;
import com.csu.tingtian.vo.FarmAndLevel;
import com.csu.tingtian.vo.ProduceComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("ProduceCommentService")
public class ProduceCommentServiceImpl implements ProduceCommentService {

    @Autowired
    private ProduceCommentMapper produceCommentMapper;
    @Autowired
    private LegalFarmMapper legalFarmMapper;


    @Override
    public CommonResponse<List<ProduceComment>> getProduceCommentByFarmName(String farmName) {
        QueryWrapper<ProduceComment> queryWrapper = new QueryWrapper<>();
        List<ProduceComment> produceCommentList = produceCommentMapper.selectList(queryWrapper.eq("farmName",farmName));
        if (produceCommentList != null){
            return CommonResponse.createForSuccess(produceCommentList);
        }else {
            return CommonResponse.createForError("未查询到商家" + farmName + "的产品评论信息");
        }

    }

    @Override
    public CommonResponse<ProduceComment> userSendCommentToProduce(ProduceComment produceComment) {
        int i = produceCommentMapper.insert(produceComment);
        if (i > -1){
            return CommonResponse.createForSuccess("插入评论成功",produceComment);
        }
        else
            return CommonResponse.createForError("插入失败");
    }

    @Override
    public CommonResponse<ProduceComment> userDeleteComment(String itemId, String userName, String time) {
        ProduceComment produceComment = new ProduceComment();
        produceComment.setItemId(itemId);
        produceComment.setUserName(userName);
        produceComment.setTime(time);

        HashMap<String, Object> map = new HashMap<>();
        map.put("itemId", itemId);
        map.put("userName", userName);
        map.put("time",time);
        int result = produceCommentMapper.deleteByMap(map);
        if (result > -1){
            return CommonResponse.createForSuccess("删除成功",produceComment);
        }
        else
            return CommonResponse.createForError("服务器异常，删除评论失败。");
    }

    @Override
    public CommonResponse<List<ProduceComment>> userGetCommentByItemId(String itemId) {
        QueryWrapper<ProduceComment> produceCommentQueryWrapper = new QueryWrapper<>();
        List<ProduceComment> produceCommentList = produceCommentMapper.selectList(produceCommentQueryWrapper.eq("itemId",itemId));
        if (produceCommentList != null){
            return CommonResponse.createForSuccess(produceCommentList);
        }
        else
            return CommonResponse.createForError("服务器异常，未查询到相关评论信息");
    }

    @Override
    public CommonResponse<List<FarmAndLevel>>returnFarmNameStarIsTwoOrLower() {
//        List<FarmAndLevel> farmAndLevelList = new ArrayList<>();
//        List<String> farmNameList = new ArrayList<>();
//        QueryWrapper<ProduceComment> commentQueryWrapper = new QueryWrapper<>();
//        List<ProduceComment> produceCommentList = produceCommentMapper.selectList(commentQueryWrapper.le("starLevel",3));
//        if (produceCommentList != null){
//            for (int i = 0; i < produceCommentList.size(); i++) {
//                String farmName;
//                farmName = produceCommentList.get(i).getFarmName();
//                farmNameList.add(farmName);
//            }
//            /***除去List重复值，且保持顺序***/
//            Set set = new HashSet();
//            List newList = new ArrayList();
//                for(Iterator iter = farmNameList.iterator();iter.hasNext();) {
//                    Object element = iter.next();
//                        if(set.add(element))
//                            newList.add(element);
//                   }
//            farmNameList.clear();
//            farmNameList.addAll(newList);
//            //然后根据farmNameList里面的名字查询农场信息
//            List<LegalFarm> legalFarmList = new ArrayList<>();
//            for (int i = 0; i < legalFarmList.size(); i++) {
//                LegalFarm legalFarm = legalFarmMapper.selectById(legalFarmList.get(i).getFarmName());
//                legalFarmList.add(legalFarm);
//            }
//
//
//
//
//
//            return CommonResponse.createForSuccess(farmAndLevelList);
//        }
//        else
//            return CommonResponse.createForError("服务器异常，未查询到2星及其以下的商家名。");
        return null;

    }


    @Override
    public int averageStarLevel(String farmName) {
        int star = 0;

        //先根据farmName找出所有的评价
        QueryWrapper<ProduceComment> queryWrapper = new QueryWrapper<>();
        List<ProduceComment> list = produceCommentMapper.selectList(queryWrapper.eq("farmName",farmName));
        if(list.size() > 0) {

            int num = produceCommentMapper.selectCount(queryWrapper.eq("farmName", farmName));

            //每条评论的starlevel加起来
            for (int i = 0; i < list.size(); i++) {
                star = star + list.get(i).getStarLevel();
            }

            //计算平均
            star = star / num;

            return star;
        }
        else
            return 5;
    }
}
